package com.easylive.admin.controller;

import com.easylive.annotation.RecordUserMessage;
import com.easylive.entity.enums.MessageTypeEnum;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.po.VideoInfoFile;
import com.easylive.entity.po.VideoInfoFilePost;
import com.easylive.entity.po.VideoInfoPost;
import com.easylive.entity.query.VideoInfoFilePostQuery;
import com.easylive.entity.query.VideoInfoFileQuery;
import com.easylive.entity.query.VideoInfoPostQuery;
import com.easylive.entity.request.videoinfo.AdminVideoInfoAuditVideoReqVO;
import com.easylive.entity.request.videoinfo.AdminVideoInfoLoadVideoListReqVO;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.VideoInfoFilePostService;
import com.easylive.service.VideoInfoFileService;
import com.easylive.service.VideoInfoPostService;
import com.easylive.service.VideoInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @Author: yudaxian
 * @DateTime: 2024/11/14 14:39
 **/
@RestController
@RequestMapping("/videoInfo")
public class VideoInfoController extends ABaseController {

    /**
     * 发布用的
     */
    @Resource
    private VideoInfoPostService videoInfoPostService;

    @Resource
    private VideoInfoFilePostService videoInfoFilePostService;

    /**
     * 查看用的
     */
    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private VideoInfoFileService videoInfoFileService;

    @PostMapping("/loadVideoList")
    public ResponseVO loadVideoList(AdminVideoInfoLoadVideoListReqVO reqVO) {

        VideoInfoPostQuery query = new VideoInfoPostQuery();
        query.setOrderBy("v.last_update_time desc");
        query.setQueryCountInfo(true);
        query.setQueryUserInfo(true);
        query.setPageNo(reqVO.getPageNo());
        query.setPageSize(reqVO.getPageSize());
        query.setVideoNameFuzzy(reqVO.getVideoNameFuzzy());
        query.setpCategoryId(reqVO.getPCategoryId());
        query.setCategoryId(reqVO.getCategoryId());
        query.setQueryRecommendInfo(reqVO.getRecommendType());

        PaginationResultVO<VideoInfoPost> listByPage = videoInfoPostService.findListByPage(query);
        return getSuccessResponseVO(listByPage);
    }

    @PostMapping("/auditVideo")
    @RecordUserMessage(messageType = MessageTypeEnum.SYS)
    public ResponseVO auditVideo(@Validated AdminVideoInfoAuditVideoReqVO reqVO) {
        videoInfoPostService.auditVideo(reqVO.getVideoId(), reqVO.getStatus(), reqVO.getReason());
        return getSuccessResponseVO(null);
    }

    @PostMapping("/recommendVideo")
    public ResponseVO recommendVideo(@Validated @NotBlank String videoId) {
        videoInfoPostService.updateRecommendType(videoId);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/deleteVideo")
    public ResponseVO deleteVideo(@Validated @NotBlank String videoId) {
        VideoInfoPost videoInfo = videoInfoPostService.getVideoInfoPostByVideoId(videoId);
        if (videoInfo == null) {
            throw new BusinessException("视频可能已经被删除");
        }
        videoInfoPostService.deleteVideoInfo(videoInfo.getUserId(), videoId);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadVideoPList")
    public ResponseVO loadVideoPList(@Validated @NotBlank String videoId) {
        VideoInfoFilePostQuery query = new VideoInfoFilePostQuery();
        query.setVideoId(videoId);
        query.setOrderBy("file_index asc");
        List<VideoInfoFilePost> list = videoInfoFilePostService.findListByParam(query);
        return getSuccessResponseVO(list);
    }


}
